<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .body-wrap{
            width: 800px;
            margin: 30px auto;
        }
        textarea{
            width: 100%;
            height: 200px;
            padding: 10px;
            outline: none;
            resize: none;
            border: 1px solid #999;
            border-radius: 12px;
            box-sizing: border-box;
        }
        button{
            background-color: rgb(41, 41, 238);
            color:#fff;
            height: 26px;
            padding: 0 20px;
            border: none;
            outline: none;
            margin: 20px 0;
            border-radius: 5px;
        }
        ul{
            list-style-type: none;
        }
        li{
            padding: 15px 10px;
            border-bottom: 1px dashed #bbb;
        }
    </style>
</head>

<body>
    <div class="body-wrap">
        <form>
            <div>
                <textarea></textarea>
            </div>
            <div>
                <button type="button">发表</button>
            </div>
        </form>

        <hr>

        <ul>
        </ul>
    </div>

</body>
<script>
    let textarea = document.querySelector('textarea');
    let btn = document.querySelector('button');
    let ul = document.querySelector('ul');

    btn.addEventListener('click',e =>{
       let text = textarea.value;
       //表单校验
       if(!text){  //if (text=""){
           alert('请输入评论内容');
           return;
       } 
       //动态的创建元素节点
       let li = document.createElement('li');
       li.innerHTML = text;

       //将创建的节点添加到页面中去
       ul.appendChild(li);

       //发表成功后 1.清空输入框
       textarea.value = '';
       //发表成功后 2.为li标签添加一个点击事件的监听
       li.addEventListener('click',e =>{
           ul.removeChild(li);
       });

    });
</script>

</html>